package com.lancoo.znbkxx.teacher.aibkTestCoach.questionToMark.adapter

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.appcompat.app.AppCompatActivity
import com.lancoo.znbkxx.teacher.R
import com.lancoo.znbkxx.teacher.aibkTestCoach.kBase.KBaseRecyclerAdapter
import com.lancoo.znbkxx.teacher.aibkTestCoach.kBase.KBaseRecyclerViewHolder
import com.lancoo.znbkxx.teacher.aibkTestCoach.networkRequest.entity.Type
import com.lancoo.znbkxx.teacher.utils.DensityUtils
import kotlinx.android.synthetic.main.layout_answersheet_child.view.*

/**
 * auther: 王国龙
 * Date：2020.06.15
 * describe:试卷预览对应的答题卡
 */
class MarkAnswerSheetChildAdapter(
        val activity: AppCompatActivity,
        val typeList: MutableList<Type>,
        var index_A: Int
) : KBaseRecyclerAdapter() {

    override fun getHolderView(parent: ViewGroup?, position: Int): View {
        return LayoutInflater.from(activity)
                .inflate(R.layout.layout_answersheet_child, parent, false)
    }

    override fun onBindViewHolder(
            holder: KBaseRecyclerViewHolder,
            position: Int
    ) {
        val index_B = position_return_index_B(position)
        val index_C = position_return_index_C(position)
        val sortIndex = typeList[index_A].QuesList[index_B].SortIndexList[index_C].SortIndex
        if (sortIndex.contains("-")) {
            holder.convertView.BtnSortIndex.layoutParams.width = DensityUtils.dp2px(50f)
            holder.convertView.BtnSortIndex.setBackgroundResource(R.drawable.aibk_bg_button_gray)
        } else {
            holder.convertView.BtnSortIndex.layoutParams.width = DensityUtils.dp2px(30f)
        }
        holder.convertView.BtnSortIndex.layoutParams.height = DensityUtils.dp2px(30f)
        holder.convertView.BtnSortIndex.text = sortIndex
        holder.convertView.BtnSortIndex.setOnClickListener {
            var index_C = position_return_index_C(position)
            clickChilidListener!!.onClick(index_A, index_B, index_C)
        }
        if (typeList[index_A].QuesList[index_B].SortIndexList[index_C].IsEval) {
            holder.convertView.v_select.visibility = View.GONE
        } else {
            holder.convertView.v_select.visibility = View.VISIBLE
        }
    }

    override fun getItemCount(): Int {
        var itemCount = 0
        var quesListSize = typeList[index_A].QuesList.size
        for (b in 0 until quesListSize) {
            itemCount += typeList[index_A].QuesList[b].SortIndexList.size
        }
        return itemCount
    }

    interface OnClickChildListener {
        fun onClick(index_A: Int, index_B: Int, index_C: Int)
    }

    private var clickChilidListener: OnClickChildListener? = null

    fun setClickChildListener(clickChilidListener: OnClickChildListener?) {
        this.clickChilidListener = clickChilidListener
    }

    private fun position_return_index_B(position: Int): Int {
        var postionX = -1
        var index_B = 0
        var QuesListSize = typeList[index_A].QuesList.size
        outLoop@ for (b in 0 until QuesListSize) {
            var SortIndexListSize = typeList[index_A].QuesList[b]
                    .SortIndexList.size
            for (c in 0 until SortIndexListSize) {
                postionX += 1
                if (position == postionX) {
                    index_B = b
                    break@outLoop
                }
            }
        }
        return index_B
    }

    private fun position_return_index_C(position: Int): Int {
        var postionX = -1
        var index_C = 0
        var QuesListSize = typeList[index_A].QuesList.size
        outLoop@ for (b in 0 until QuesListSize) {
            var SortIndexListSize = typeList[index_A].QuesList[b]
                    .SortIndexList.size
            for (c in 0 until SortIndexListSize) {
                postionX += 1
                if (position == postionX) {
                    index_C = c
                    break@outLoop
                }
            }
        }
        return index_C
    }

}